package com.cz.run.bumng;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.run.beans.mysql.BumngUser;
import com.cz.run.beans.mysql.Product;
import com.cz.run.beans.mysql.ProductLuckDraw;
import com.cz.run.bumng.model.PageModel;
import com.cz.run.bumng.model.Result;
import com.cz.run.service.IProductExchangeRecordService;
import com.cz.run.service.IProductLuckDrawService;
import com.cz.run.service.IProductService;
import com.cz.run.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by deior on 2018/6/9.
 */
@Controller
@RequestMapping("/bumng")
public class ProductController {

    @Autowired
    private IProductService iProductService;

    @Autowired
    private IProductLuckDrawService iProductLuckDrawService;

    @Autowired
    private IProductExchangeRecordService iProductExchangeRecordService;

    @RequestMapping(value ="/productList.htm",method = RequestMethod.GET)
    public String productList(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);

        String name = request.getParameter("name");
        model.addAttribute("name",name);

        PageModel pageModel =  PageUtil.getPageData(request);

        EntityWrapper<Product> ew = new EntityWrapper<>();
        ew.where("1 = 1").and(StringUtils.hasLength(name),"name = {0}",name)
                .orderBy("create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());
        List<Product> products =  iProductService.selectList(ew);

        EntityWrapper<Product> ew1 = new EntityWrapper<>();
        ew1.where("1 = 1").and(StringUtils.hasLength(name),"name = {0}",name)
                ;
        int count =  iProductService.selectCount(ew1);

        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);

        model.addAttribute("products",products);

        return "productList";
    }

    @RequestMapping(value = "editProduct.htm")
    public String editNews(HttpServletRequest request, Model model) {
        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);
        Product product = new Product();
        String id =request.getParameter("id");
        if(StringUtils.hasLength(id)){
            EntityWrapper<Product> ew = new EntityWrapper<>();
            ew.where("id = {0}",id);
             product =iProductService.selectOne(ew);

            model.addAttribute("type","1");
        }else{
            model.addAttribute("type","2");
        }

        EntityWrapper<ProductLuckDraw> luew = new EntityWrapper<>();
        luew.where("product_id = {0}",product.getId());
        ProductLuckDraw productLuckDraw =iProductLuckDrawService.selectOne(luew);


        if(null!=productLuckDraw){
            model.addAttribute("time",productLuckDraw.getPrizeTime());
        }

        model.addAttribute("product",product);

        return "editProduct";
    }


    @RequestMapping(value = "addProduct.do", method = RequestMethod.POST)
    @ResponseBody
    public Object addNews(HttpServletRequest request, @ModelAttribute Product product, Model model) {


        if(StringUtils.isEmpty(product.getName())||StringUtils.isEmpty(product.getCostPrice())||StringUtils.isEmpty(product.getMarketPrice())||StringUtils.isEmpty(product.getPrice())||
                StringUtils.isEmpty(product.getStock())||StringUtils.isEmpty(product.getWarningStock())||StringUtils.isEmpty(product.getIntegral())||StringUtils.isEmpty(product.getPictureUrl())
                ||StringUtils.isEmpty(product.getContent())){

            return  new Result(false,"参数不能为空");
        }

        String timeStr = request.getParameter("time");

        if("2".equals(product.getType())){
            if(StringUtils.isEmpty(timeStr)){
                return  new Result(false,"开奖不能为空");
            }

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = null;

                try {
                    time = format.parse(timeStr);
                } catch (ParseException e) {
                    e.printStackTrace();
                    return  new Result(false,"冻结时间格式转化异常");
                }

            EntityWrapper<ProductLuckDraw> luew = new EntityWrapper<>();
            luew.where("product_id = {0}",product.getId());
            ProductLuckDraw productLuckDraw =iProductLuckDrawService.selectOne(luew);
            if(null==productLuckDraw){
                productLuckDraw = new ProductLuckDraw();
                productLuckDraw.setPrizeTime(time);
                productLuckDraw.setProductId(product.getId());
                iProductLuckDrawService.insert(productLuckDraw);
            }else{
                productLuckDraw.setPrizeTime(time);
                productLuckDraw.setProductId(product.getId());
                iProductLuckDrawService.updateById(productLuckDraw);
            }
        }


        product.setUpdateTime(new Date());
        if(product.getId()==null) {
            product.setCreateTime(new Date());
            iProductService.insert(product);
        }else{
            EntityWrapper<Product> ew = new EntityWrapper<>();
            ew.where("id = {0}",product.getId());
            iProductService.update(product,ew);
        }

        return  new Result(true,"成功");
    }

    @RequestMapping(value = "updateStatus.do", method = RequestMethod.POST)
    @ResponseBody
    public Object updateStatus(HttpServletRequest request,  Model model) {


        String id =request.getParameter("id");
        String status =request.getParameter("status");

            EntityWrapper<Product> ew = new EntityWrapper<>();
            ew.where("id = {0}",id);
            Product product =iProductService.selectOne(ew);
            product.setStatus(Integer.parseInt(status));
            iProductService.update(product,ew);

        return  new Result(true,"成功");
    }


    @RequestMapping(value ="/productRecordList.htm",method = RequestMethod.GET)
    public String productRecordList(HttpServletRequest request, @ModelAttribute BumngUser bumngUser, Model model){

        BumngUser bumngUser1 = (BumngUser) request.getSession().getAttribute("bumngUser");

        if(null==bumngUser1){
            return "login";
        }
        model.addAttribute("bumngUser",bumngUser1);

        String name = request.getParameter("name");
        model.addAttribute("name",name);

        PageModel pageModel =  PageUtil.getPageData(request);

        EntityWrapper<Product> ew = new EntityWrapper<>();
        ew.where("1 = 1").and(StringUtils.hasLength(name),"name = {0}",name)
                .orderBy("create_time desc limit "+pageModel.getPageNum()+","+pageModel.getPageSize());
        List<Product> products =  iProductService.selectList(ew);

        EntityWrapper<Product> ew1 = new EntityWrapper<>();
        ew1.where("1 = 1").and(StringUtils.hasLength(name),"name = {0}",name)
        ;
        int count =  iProductService.selectCount(ew1);

        model.addAttribute("pageTotal",count%pageModel.getPageSize()>0?count/10+1:count/10);
        model.addAttribute("pageNum",(pageModel.getPageNum())/10+1);

        model.addAttribute("products",products);

        return "productList";
    }



}
